layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;
	var upload = layui.upload;

	var table_filter = 'pikuanxinxi_list',
		where_json = {
			_o: 'pkxx_id',
			_b: 'desc'
		},
		form_filter = 'pikuanxinxi_search_form';
	// 渲染表格
	if (load_data === true) {
		table.render({
			elem: '#' + table_filter,
			id: table_filter,
			url: pikuanxinxi_list_url,
			method: 'post',
			height: 'full-200',
			where: {
				_o: 'pkxx_id',
				_b: 'desc'
			},
			cols: [pikuanxinxi_cols],
			page: {
				layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
				limit: 30,
				theme: '#393d49',
				groups: 8
			},
			autoSort: false,
			toolbar: '#pikuanxinxiToolbar',
			initSort: {
				field: 'pkxx_id',
				type: 'desc'
			}
		});
	}



	// 编辑表格
	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		switch (e) {
			case 'del':
				layer.confirm('确定删除吗？', {
					icon: 3
				}, function(index) {
					send_info(delete_url, {
						pkxx_id: data.pkxx_id
					}, (data) => {
						layer.msg('删除成功', {
							icon: 1
						});
						table.reload(table_filter);
					})
				})
				break;
			case 'edit':
				create_tab($(this));
				break;
			default:
				break;
		}
	})

	upload.render({
		elem: '#implodeXx2',
		url: upload_xx,
		acceptMime: 'application/vnd.ms-excel',
		auto: true,
		accept: 'file',
		exts: 'xls',
		multiple: false,
		drag: false,
		before: function(obj) {
			layer.load();
		},
		done: function(res, index, upload) {
			if (res.code == 0) {
				layer.closeAll('loading');
				layer.msg(res.msg, {
					icon: 3
				});
				return false;
			} else {
				layer.closeAll('loading');
				layer.msg('导入成功', {
					icon: 1
				})
				table.reload(table_filter);

			}

		},
		error: function(index, upload) {
			layer.msg('excel格式错误', {
				icon: 2
			});
			layer.closeAll('loading');
		}
	});

	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		var filter_url = $(this).attr('data-url');
		switch (obj.event) {
			case 'implodeXx':
				$('#implodeXx2').click()
				break;
			case 'del':
				var checkStatus = table.checkStatus(table_filter);
				var tr = checkStatus.tr;
				if (checkStatus.data.length == 0) {
					layer.msg('请选择要删除的数据', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				var id_arr = new Array();
				layer.confirm('确定删除选中数据吗？', function(index) {
					checkStatus.data.map(function(obj, i) {
						id_arr.push(obj.pkxx_id);
					});
					send_info(delete_url, {
						pkxx_id: id_arr
					}, function(data) {
						if (data.code == 0) {
							tr.map(function(obj, i) {
								$(obj.selector).remove();
							})
							layer.msg('删除成功', {
								icon: 1
							});
							table.reload(table_filter);
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
		}
	})


	form.render();

	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		where_json[name] = value;
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	});

	form.on('select', function(data) {
		where_json[data.elem.attributes['lay-filter'].value] = data.value;
	})



	form.on('submit(pikuanxinxi_search_form)', function(data) {
		layer.load();
		table.reload(table_filter, {
			where: where_json,
			page: {
				curr: 1
			},
			done: function(res, curr, count) {
				layer.closeAll();
			}
		});
		return false
	})

})